import aiohttp
import asyncio
from utils.HBaseConnect import HBaseConnect
import uuid
import time
import schedule

from utils.MysqlConnect import MysqlConnect

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 "
                  "Safari/537.36 Edg/125.0.0.0",
    "Cookie": "ttwid=1%7CBkXXzXO31z4qByseVmOrVEskhVUIHe2xkwEIxhiIvNo%7C1671437658"
              "%7Cd16fc1a705a692546ccf35abc30bdf597ff8f75efc808594ecea83225f8709a9; LOGIN_STATUS=1; "
              "store-region=cn-gx; store-region-src=uid; __live_version__=%221.1.1.1623%22; pwa2=%220%7C0%7C3%7C0%22; "
              "bd_ticket_guard_client_web_domain=2; "
              "passport_assist_user=Cjyt4QBnddZ-nN3HQOFhfVP_cTcuxL0dKvrwO-DfIn"
              "-tfb7EuEWTEopioAlnyvrfTyFMgiapsk_7Roc0pwcaSgo8zf5-GqFdD6vg6jigRxfk"
              "-WlZEX7SdLabMwB3Pz8zHkUgJhcOTAXg7gZeDsdy9z2WEZVtaVs4xX342-RqEMG2yg0Yia_WVCABIgEDzz6KMA%3D%3D; "
              "n_mh=5px6dww4nV4PH94ROCeYcankMz96abagYUYgfT2hMp0; sso_uid_tt=7b974ae211f745842046a23d9e52f2d5; "
              "sso_uid_tt_ss=7b974ae211f745842046a23d9e52f2d5; toutiao_sso_user=095b9dea9e4463ca791581d71a73e86d; "
              "toutiao_sso_user_ss=095b9dea9e4463ca791581d71a73e86d; uid_tt=7b974ae211f745842046a23d9e52f2d5; "
              "uid_tt_ss=7b974ae211f745842046a23d9e52f2d5; sid_tt=095b9dea9e4463ca791581d71a73e86d; "
              "sessionid=095b9dea9e4463ca791581d71a73e86d; sessionid_ss=095b9dea9e4463ca791581d71a73e86d; "
              "SEARCH_RESULT_LIST_TYPE=%22single%22; passport_csrf_token=91b83aceda2fc9426203be6a888cb66b; "
              "passport_csrf_token_default=91b83aceda2fc9426203be6a888cb66b; douyin.com; "
              "xg_device_score=7.802204888412783; device_web_cpu_core=12; device_web_memory_size=8; "
              "architecture=amd64; dy_swidth=1536; dy_sheight=864; "
              "publish_badge_show_info=%220%2C0%2C0%2C1717382427784%22; strategyABtestKey=%221717382427.885%22; "
              "csrf_session_id=24237f2d80c857066564b5c368afda80; "
              "s_v_web_id=verify_lwyd5mlh_lden6Aik_0nDi_4600_92if_BCiRN1netIHk; "
              "sid_ucp_sso_v1=1.0.0"
              "-KDZlMWI0ZTY5MzBiZTgzMTY1MWI2ZmYxNmFhYTYwZjlmYzc4YmU0ZTkKHQjKkKDU8wIQntr0sgYY7zEgDDC61drYBTgGQPQHGgJobCIgMDk1YjlkZWE5ZTQ0NjNjYTc5MTU4MWQ3MWE3M2U4NmQ; ssid_ucp_sso_v1=1.0.0-KDZlMWI0ZTY5MzBiZTgzMTY1MWI2ZmYxNmFhYTYwZjlmYzc4YmU0ZTkKHQjKkKDU8wIQntr0sgYY7zEgDDC61drYBTgGQPQHGgJobCIgMDk1YjlkZWE5ZTQ0NjNjYTc5MTU4MWQ3MWE3M2U4NmQ; sid_guard=095b9dea9e4463ca791581d71a73e86d%7C1717382430%7C5184001%7CFri%2C+02-Aug-2024+02%3A40%3A31+GMT; download_guide=%223%2F20240603%2F0%22; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0%7D; __ac_nonce=0665d32c90041df0d67c9; __ac_signature=_02B4Z6wo00f01zisSSAAAIDC5.e6buoPUNs4jE2AAKiAgd0r0h9nQ0EUhPsNoXyvvgXCBUp0PWG-1yRvQfuZP.4XiNqCdBax-zElt7OsLMdZzfsDMg2frNF6cVdVDKmYY3TQ7SOvuzQwwc9K07; passport_fe_beating_status=true; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1536%2C%5C%22screen_height%5C%22%3A864%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A12%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A7.35%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A100%7D%22; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAARPs3i8FpNurXYVvc2EF2EbfMYOV2ESZXFAArBjjw1Oo%2F1717430400000%2F0%2F0%2F1717385349060%22; FOLLOW_NUMBER_YELLOW_POINT_INFO=%22MS4wLjABAAAARPs3i8FpNurXYVvc2EF2EbfMYOV2ESZXFAArBjjw1Oo%2F1717430400000%2F0%2F1717384749060%2F0%22; home_can_add_dy_2_desktop=%221%22; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCUGxGcnNtcWxsQjdVbVQzc1VLTEFjeXExTnB2R3U0bUZBV082UkJSRldENHlvSFVtTUtGZU5YdGtkYTNTRFRROTNYYjVCQk11OWNCMmdNRUhyNm1TaUk9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D; msToken=BzGjn0vDZByDGX8DU486jIjWiXbsNQJnVQG5cn6VP-yFVBJIluDMZ_s3Y-CUoGt9aya8r9YO65-tl5NPmB9cuM06SyMNfJG1pN0Cv0nBAWQ9eFhARF7ZHhEQ_YJQ7XSX; WallpaperGuide=%7B%22showTime%22%3A1717382742963%2C%22closeTime%22%3A1717383634103%2C%22showCount%22%3A1%2C%22cursor1%22%3A33%2C%22cursor2%22%3A0%2C%22hoverTime%22%3A1717383632174%7D; odin_tt=76bb0cb20c93731c6d87b95f6c4cf3f03f423871047cdd7f49d57b1e6b2f1c5e804827cdfd17acb070a29126925d704a40aeecd847343ec3f83b30a344452be5; IsDouyinActive=true",
}
tableName = "hot_search_keywords"
url = ("https://www.douyin.com/aweme/v1/web/hot/search/list/?device_platform=webapp&aid=6383&channel=channel_pc_web"
       "&detail_list=1&source=6&pc_client_type=1&version_code=170400&version_name=17.4.0&cookie_enabled=true"
       "&screen_width=1536&screen_height=864&browser_language=zh-CN&browser_platform=Win32&browser_name=Edge"
       "&browser_version=125.0.0.0&browser_online=true&engine_name=Blink&engine_version=125.0.0.0&os_name=Windows"
       "&os_version=10&cpu_core_num=12&device_memory=8&platform=PC&downlink=7.35&effective_type=4g&round_trip_time"
       "=100&webid=7178769978400474639&msToken=SE1NK16iQxCkRVlGwZY4d9Ifz_wKI87vzEqvRPGU8s-zsydLZRMgk"
       "-aJvwGPaGH3QJsSSknhCIrZYwT6kIV4znW4A87swem_oVyYT6-NfeQETo-RRIuWzXjyFCzhGBee&a_bogus=Ej80"
       "%2FmzvmDDTkDWf5XxLfY3q64q3Y80Y0trEMD2fYVvc5639HMOG9exoVykvY3ujLG%2FlIb6jy4hSYpHMic%2FjA3v6HSRKl2np-g00t"
       "-P2so0j5ZhjCfuDrzfF-vzWt-Bd-Jd3iQ4hy7deFuRplnAJ5k1cthMeaA6%3D&verifyFp"
       "=verify_lwyd5mlh_lden6Aik_0nDi_4600_92if_BCiRN1netIHk&fp=verify_lwyd5mlh_lden6Aik_0nDi_4600_92if_BCiRN1netIHk")

def save_hot_search_keywords(hbase,mysql, dic):
    print("排名" + dic['rank'], dic)
    hbase.putTable(tableName, rowKey=str(uuid.uuid1().hex), data={
        "info:keyword": dic['keyword'],
        "info:num": dic['searchNum'],
        "info:rank": dic['rank'],
        "info:url": dic['url'],
        "info:platform": "抖音",
        "info:time": dic['time'],
    })
    insert_sql = (
        "INSERT INTO information(hot_keyword, search_num, ranking, url, platform,time) "
        "VALUES(%s, %s, %s, %s, %s, %s)")
    insert_args = (dic['keyword'], dic['searchNum'], dic['rank'], dic['url'],"抖音",dic['time'])
    mysql.insert(insert_sql, insert_args)


# 获取热搜关键词
async def get_hot_search_keywords():
    try:
        # 初始化hbase的参数
        hbase = HBaseConnect()
        hbase.start()
        mysql = MysqlConnect()
        current_time = time.strftime('%Y-%m-%d %H:%M:%S')
        async with aiohttp.ClientSession(headers=headers) as session:
            async with session.get(url) as res:
                resp = await res.json()
                dic = {"time": current_time}
                items = resp['data']['word_list']
                for item in items:
                    if item.get('position') and item['position'] != '':
                        dic['keyword'] = item["word"]
                        dic['searchNum'] = str(item["hot_value"])
                        dic['url'] = 'https://www.douyin.com/hot/'+item["sentence_id"]
                        dic['rank'] = str(item['position'])
                        save_hot_search_keywords(hbase,mysql, dic)
                hbase.stop()
                # 关闭mysql
                mysql.cur.close()
                mysql.connection.close()
    except Exception as e:
        print("Exception", e)


def run_asyncio_task():
    print("当前时间", time.strftime('%Y-%m-%d %H:%M:%S'))
    asyncio.run(get_hot_search_keywords())


if __name__ == '__main__':
    # 在每小时的00, 10, 20, 30, 40, 50分钟运行任务
    schedule.every().hour.at(":00").do(run_asyncio_task)
    schedule.every().hour.at(":10").do(run_asyncio_task)
    schedule.every().hour.at(":20").do(run_asyncio_task)
    schedule.every().hour.at(":30").do(run_asyncio_task)
    schedule.every().hour.at(":40").do(run_asyncio_task)
    schedule.every().hour.at(":50").do(run_asyncio_task)
    while True:
        schedule.run_pending()
        time.sleep(1)
